import 'package:flutter/material.dart';
import 'package:flutter_app/generated/l10n.dart';
import 'package:flutter_app/provider/locale_provider.dart';
import 'package:flutter_app/utils/application.dart';
import 'package:flutter_app/utils/shared_preferences.dart';
import 'package:provider/provider.dart';
import 'package:flutter_app/components/layout.dart';
class LanguageSetting extends StatefulWidget {
  @override
  _LanguageSettingState createState() => _LanguageSettingState();
}

class _LanguageSettingState extends State<LanguageSetting> {
  final List<String> _list = <String>['跟随系统', '中文', 'English'];

  @override
  Widget build(BuildContext context) {
    final String locale =  Application.sharePreference.getString(SharedPreferencesKeys.Locale);
    String localeMode;
    switch (locale) {
      case 'zh':
        localeMode = _list[1];
        break;
      case 'en':
        localeMode = _list[2];
        break;
      default:
        localeMode = S.of(context).autoBySystem;
        break;
    }

    return Scaffold(
        appBar:DarkHeadBar(S.of(context).language_setting),
        body: SafeArea(
          child: ListView.separated(
            itemCount: _list.length,
            separatorBuilder: (_, __) => const Divider(),
            itemBuilder: (_, int index) {
              return InkWell(
                onTap: () {
                  final String locale =
                      index == 0 ? '' : (index == 1 ? 'zh' : 'en');
                  // context.read<LocaleProvider>().setLocale(locale);
                  Provider.of<LocaleProvider>(context, listen: false).setLocale(locale);
                },
                child: Container(
                  alignment: Alignment.centerLeft,
                  padding: const EdgeInsets.symmetric(horizontal: 16.0),
                  height: 50.0,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(_list[index]),
                      ),
                      Opacity(
                        opacity: localeMode == _list[index] ? 1 : 0,
                        child: const Icon(Icons.done, color: Colors.blue),
                      )
                    ],
                  ),
                ),
              );
            },
          ),
        ));
  }
}
